package org.jsola.hr.web;

import org.jsola.core.Page;
import org.jsola.core.Result;
import org.jsola.exception.ParamException;
import org.jsola.hr.constant.HrConstants;
import org.jsola.hr.core.PushConfig;
import org.jsola.hr.dto.NewsInfoUpdateDTO;
import org.jsola.hr.dto.NewsTypeAddDTO;
import org.jsola.hr.dto.NewsTypeUpdateDTO;
import org.jsola.hr.query.NewsTypeQuery;
import org.jsola.hr.service.INewsTypeService;
import org.jsola.hr.vo.NewsTypeListVO;
import org.jsola.hr.vo.NewsTypeVO;
import org.jsola.user.core.TokenUser;
import org.jsola.user.core.UserContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import javax.validation.Valid;


/**
 * 官网信息的类型
 *
 * @author zhr
 */
@RestController("hrNewsTypeController")
@RequestMapping(HrConstants.API_PATH + "/v1/newsTypes")
public class NewsTypeController implements INewsTypeControllerAPI {

    @Autowired
    private INewsTypeService newsTypeService;

    @Autowired
    private PushConfig pushConfig;

    @Override
    @PostMapping
    public Result<NewsTypeVO> save(@Valid @RequestBody NewsTypeAddDTO newsTypeAddDTO) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 新增官网信息的类型权限校验

        return Result.success(
                newsTypeService.save(newsTypeAddDTO, tokenUser)
        );
    }


    @Override
    @PutMapping
    public Result<Integer> update(@Valid @RequestBody NewsTypeUpdateDTO newsTypeUpdateDTO) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 修改官网信息的类型权限校验

        int count = newsTypeService.update(newsTypeUpdateDTO, tokenUser);
        if (count == 0) {
            throw new ParamException("更新失败，请刷新页面后重试");
        }
        return Result.success(count);
    }


    @Override
    @DeleteMapping(value = "/{newsTypeId}")
    public Result<Integer> delete(@PathVariable String newsTypeId) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 彻底删除官网信息的类型权限校验

        int count = newsTypeService.deleteByIds(tokenUser, newsTypeId);
        if (count == 0) {
            throw new ParamException("删除失败，请刷新页面后重试");
        }
        return Result.success(count);
    }

    @Override
    @DeleteMapping
    public Result<Integer> deleteBatch(@RequestBody String[] newsTypeIds) {
        // todo 批量彻底删除官网信息的类型权限校验

        int count = newsTypeService.deleteTypeAndInfoById(newsTypeIds);
        if (count == 0) {
            throw new ParamException("删除失败，请刷新页面后重试");
        }
        return Result.success(count);
    }

    @Override
    @GetMapping(value = "/{newsTypeId}")
    public Result<NewsTypeVO> findById(@PathVariable String newsTypeId) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看官网信息的类型权限校验

        return Result.success(
                newsTypeService.selectById(newsTypeId, tokenUser.getSiteId())
        );
    }

    @Override
    @GetMapping(value = "/listAll")
    public Result<List<NewsTypeListVO>> listAll(@Valid NewsTypeQuery newsTypeQuery) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看官网信息的类型权限校验

        return Result.success(
                newsTypeService.select(newsTypeQuery, tokenUser.getSiteId())
        );
    }


    @Override
    @GetMapping
    public Result<Page<NewsTypeListVO>> page(@Valid NewsTypeQuery newsTypeQuery) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看项目权限校验

        return Result.success(
                newsTypeService.selectPage(newsTypeQuery, tokenUser.getSiteId())
        );
    }


    @Override
    @GetMapping(value = "/selectNewsType")
    public Result<Page<NewsTypeListVO>> pageTypeByCodeAndType(@Valid NewsTypeQuery newsTypeQuery) {
        // todo 查看项目权限校验

        TokenUser tokenUser = UserContext.getUser();

        return Result.success(
                newsTypeService.selectPageNewsType(newsTypeQuery)
        );
    }

    @Override
    @PostMapping(value = "/insertNewsTypeAndInfo")
    public Result<NewsTypeVO> insertNewsTypeAndInfo(@Valid @RequestBody NewsTypeAddDTO newsTypeAddDTO) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看项目权限校验

        return Result.success(
                newsTypeService.insertNewsTypeAndInfo(newsTypeAddDTO, tokenUser)
        );
    }

    @Override
    @PutMapping(value = "/updateNewsTypeAndInfo")
    public Result<Integer> updateNewsTypeAndInfo(@Valid @RequestBody NewsInfoUpdateDTO newsInfoUpdateDTO) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看项目权限校验

        return Result.success(
                newsTypeService.updateNewsTypeAndInfo(newsInfoUpdateDTO, tokenUser)
        );
    }
}

